<template>
<div class="login-wrap">
  <div class="background">
    <img :src="imgBackSrc" width="100%" height="100%" alt="" />
  </div>
  <div class="front-main">
    <el-container>
      <el-header height="140px" class="header-wrap">
        <el-row>
          <el-col :span="10" :offset="3">
           <p>
             <img :src="imgSrc" height="100px" class="middle">
             公寓管理系统
           </p>
          </el-col>
        </el-row>
      </el-header>
      <el-main style="height: calc(100% - 140px);">
        <div class="form-wrap">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRule">
            <el-form-item>
              <div class="welcome-font">欢迎登录</div>
              <el-divider></el-divider>
            </el-form-item>
            <el-form-item prop="username" required>
              <el-input v-model="loginForm.username" placeholder="请输入账号" >
                <template slot="prepend">
                  <i class="el-icon-user-solid"></i>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password" required>
              <el-input v-model="loginForm.password" placeholder="请输入密码"  type="password">
                <template slot="prepend">
                  <i class="el-icon-edit"></i>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="Code">
              <el-row>
                <el-col :span="17">
                  <el-input class="input" maxlength="8" placeholder="请输入验证码" v-model="loginForm.Code"></el-input>
                </el-col>
                <el-col :span="7">
                  <div class="divIdentifyingCode" @click="getIdentifyingCode">
                    <img :src="vsCode" style="height:40px; width: 100px; cursor: pointer;" alt="点击更换" title="点击更换" />
                  </div>
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item>
              <div type="text" class="registerFont" @click="register"><i class="el-icon-video-play" style="color: red;margin-right: 5px"></i>立即注册</div>
            </el-form-item>
            <el-form-item>
              <el-button @click="login">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
</div>
</div>
</template>

<script>

  export default {
        name: "login",
    data(){
        return{
          loginForm:{
            username:'',
            password:'',
            Code:''
          },
          loginRule:{
              username: [
                { required: true, message: '请输入账号', trigger: 'blur'},
                { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
              ],
              password: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 1, max: 9, message: '长度在 1 到 9 个字符', trigger: 'blur' }

              ],
                Code: [
                { required: true, message: '请输入验证码', trigger: 'blur' },
              ],
          },
          imgSrc:require('../assets/images/logo.jpg'),
          imgBackSrc:require('../assets/images/apartmentBackground.jpg'),
          vsCode:'http://localhost:8888/apartment/util/getCodeImg',
          vsCodeVerify:'',
        }
    },
    methods: {
        async getIdentifyingCode(){
          let id = Math.ceil(Math.random() * 100);
          this.vsCode = 'http://localhost:8888/apartment/util/getCodeImg?id=' + id
        },
        //拿取二维码的值
        async getCode(){
          const {data: res} = await this.$http.get('/util/getCode')
          this.vsCodeVerify = res.message
        },
        login() {
          this.getCode()
          this.$refs.loginForm.validate(async valid => {
            //如果验证不成功直接返回 false
            if (!valid) return false
            const { data: res } = await this.$http.post('/user/login', this.loginForm)
            console.log(res)
            if (res.code !== 200) return this.$message.error('登录失败')
            if(this.vsCodeVerify != this.loginForm.Code) return this.$message.error('验证码输入错误')
            this.$message.success('登录成功')

            //将 token 存入浏览器缓存
            window.localStorage.setItem('token', res.data.token)
            window.localStorage.setItem('username', res.data.username)
            //进行路由跳转
            console.log('this')
            this.$router.push({path:'/home'})
          })
        },
        register(){
          this.$router.push('/register')
        }
      }
    }
</script>
<style scoped>
  .el-input__inner {
    height: 50px;
    font-size: 20px;
    box-shadow: none;
    border: 1px solid #e9e9e9;
  }
  .el-button{
    height: 50px;
    font-size: 20px;
    box-shadow: none;
    border: 1px solid #e9e9e9;
    background-color: red;
    color: #FFFFFF;
    font-family: SimSun;
    font-weight: bold;
    width: 100%;
  }
  .el-main{
    padding: 0px;
  }
</style>



<style scoped>
.registerFont{
  color: red;
  font-size: 20px;
  cursor:pointer
}
.registerFont:hover{
  color: crimson;
}
  .login-wrap{
    height: 100%;
    width: 100%;
  }
.background{
  z-index:-1;
  width: 100%;
  height: 100%;
  position: absolute;
  overflow-y: hidden;
}
  .front-main{
    z-index:1;
    background-image: linear-gradient(to bottom right, rgba(227,228,229,1), rgba(227,228,229,0));
    height: 100%;
    width: 100%;
  }
  .header-wrap{
    background-color: #ffffff;
    height: 150px;
    font-family: SimSun;
    font-weight: bold;
    text-shadow: 2px 2px 5px honeydew;
    letter-spacing: 3px;
    font-size: 35px;
    margin-top: -35px;
    height: 100%;
    width: 100%;
  }
  img.middle{
    vertical-align: middle;
  }
  .form-wrap{
    height: 450px;
    width: 400px;
    background-color: #ffffff;
    padding: 30px;
    margin-left: 60%;
    margin-top: 10%;
    opacity:0.9;
    border-radius:5px;
  }
  .welcome-font{
    font-family: SimSun;
    font-weight: bold;
    text-shadow: 2px 2px 5px honeydew;
    letter-spacing: 3px;
    font-size: 30px;
    text-align: center;
  }
.divIdentifyingCode {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 5;
  width: 102px; /*设置图片显示的宽*/
  height: 40px; /*图片显示的高*/
  background: #e2e2e2;
  margin: 0;
}
</style>
